'use strict';

const Controller = require('egg').Controller;
const { getCookieValue } = require('../public/processCookies');
/**
* @controller VlanController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class VlanController extends Controller {
  async list() {
    /**
        * @summary vlanlist。
        * @description vlanlist。
        * @router get /vlanlist
        * @request query integer limit
        * @request query integer page
        * @request query String OltId
        * @request query String search
        */
    const params = this.ctx.query;
    const result = await this.ctx.service.vlan.list(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result.rows,
      count: result.count,
    };
  }
  async create() {
    /**
    * @summary vlancreate。
    * @description vlancreate。
    * @router post /vlancreate
    * @request body vlancreate
    */
    const params = this.ctx.request.body;
    const result = await this.ctx.service.vlan.create(params);
    const oltResult = await this.ctx.model.Olt.findOne({_id: params.oltId});
    global.log.desc = `${oltResult.name} ${oltResult.oltIP} create vlan `;
    global.log.after = `addvlan:${params.VlanID} Memo:${params.Memo || null}`;
    this.ctx.body = {
      data: result,
      status:200
    };
  }
  async delete() {
    /**
        * @summary vlandelete。
        * @description vlandelete。
        * @router get /vlandelete
        * @request query string id
        */
    const params = this.ctx.query;
    const vlanResult = await this.ctx.model.Vlan.findOne({_id: params.id});
    const oltResult = await this.ctx.model.Olt.findOne({_id: vlanResult.oltId});
    const result = await this.ctx.service.vlan.delete(params);
    global.log.desc = `${oltResult.name} ${oltResult.oltIP} delete vlan ${vlanResult.VlanID}`;
    this.ctx.body = {
      data: result,
      status:200
    };
  }
  async listAll() {
    /**
        * @summary vlanlistall。
        * @description vlanlistall。
        * @router get /vlanlistall
        * @request query string OltId
        */
    const params = this.ctx.query;
    const result = await this.ctx.service.vlan.listAll(params);
    this.ctx.body = {
      data: result,
      status: 200,
      msg: '',
    };
  }
  async edit() {
    /**
        * @summary vlaneditmemo
        * @description vlaneditmemo
        * @router post /vlanedit
        * @request body string vlanID
        */
    const params = this.ctx.request.body;
    const vlanResult = await this.ctx.model.Vlan.findOne({_id: params.id });
    const result = await this.ctx.service.vlan.edit(params);
    global.log.desc = `edit vlan`;
    global.log.before = `{Memo:${vlanResult.description || null}}`;
    global.log.after = `{Memo:${params.description}}`;
    this.ctx.body = {
      data: result,
      status:200
    };
  }
  async vlanverify() {
    /**
        * @summary vlaneditmemo
        * @description vlaneditmemo
        * @router get /vlanverify
        * @request query string VlanID
        * @request query string oltId
        */
    const params = this.ctx.query;
    const result = await this.ctx.service.vlan.vlanverify(params);
    this.ctx.body = {
      msg: result.msg,
      data: result.flag,
    };
  }
}

module.exports = VlanController;
